const CryptoJS = require('crypto-js');
const myCrypto = require('./myCrypto.js');

var method = "CETCITYPERIOD";

// 用CryptoJS库来创建一个Base64编码器，他拥有一个encode方法，用来将字符串编码为Base64字符串。
// class Base64 {
//     // 编码
//     encode(str) {
//         return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(str))
//     }
//     // 解码
//     decode(str) {
//         return CryptoJS.enc.Base64.parse(str).toString(CryptoJS.enc.Utf8)
//     }
// }



var akb36 = "mAkJqt8coXQ96zML"
var akb35 = "=qoKNLgdAjJbU8zx"
var akb34 = "N4EDAQpO2ejqgCoX"
var akb48 = "t4ABRmeN"


var getParam = (function() {
    var AES = {
        encrypt: function(text) {
            var secretkey = (CryptoJS.MD5(akb34).toString()).substr(16, 16);
            var secretiv = (CryptoJS.MD5(akb35).toString()).substr(0, 16);
            secretkey = CryptoJS.enc.Utf8.parse(secretkey);
            secretiv = CryptoJS.enc.Utf8.parse(secretiv);
            var result = CryptoJS.AES.encrypt(text, secretkey, {
                iv: secretiv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            return result.toString()
        },
    };
    var DES = {
        encrypt: function(text) {
            var secretkey = (CryptoJS.MD5(akb36).toString()).substr(0, 16);
            var secretiv = (CryptoJS.MD5(akb48).toString()).substr(24, 8);
            secretkey = CryptoJS.enc.Utf8.parse(secretkey);
            secretiv = CryptoJS.enc.Utf8.parse(secretiv);
            var result = CryptoJS.DES.encrypt(text, secretkey, {
                iv: secretiv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            return result.toString()
        },
    };

    function ObjectSort(obj) {
        var newObject = {};
        Object.keys(obj).sort().map(function(key) {
            newObject[key] = obj[key]
        });
        return newObject
    }

    function MyEncode(str) {
        var arr = "32223".split('')
        var b = new myCrypto.Base64();
        arr.forEach(times=>{
            switch (times) {
            case "1":
                str = AES.encrypt(str)
                break;
            case "2":
                str = DES.encrypt(str)
                break;
            case "3":
                str = b.encode(str)
                break;
            }
        }
        )
        return str;
    }

    return function(method, obj) {
        var appId = '4f0e3a273d547ce6b7147bfa7ceb4b6e';
        var timestamp = new Date().getTime();
        var need = {
            appId: appId,
            method: method,
            timestamp: timestamp,
            clienttype: 'WEB',
            object: obj,
            secret: hex_md5(appId + method + timestamp + 'WEB' + JSON.stringify(ObjectSort(obj)))
        };
        return MyEncode(JSON.stringify(need))
    }
}
)();

// CryptoJS使用md5
function hex_md5(s) {
    return CryptoJS.MD5(s).toString(CryptoJS.enc.Hex);
}

function mygetParam(obj1) {
    return getParam(method, obj1)
}
// var param = getParam(method, obj)
//
// console.log(param)
